package redis.redis.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import redis.redis.entity.User;
import redis.redis.service.IUserService;
import redis.redis.util.Message;
import redis.redis.util.RedisUtil;
import redis.redis.util.Result;
import redis.redis.util.StatusCode;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-06-08
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IUserService iUserService;

    //解决跨域问题
    //    @CrossOrigin
//    @RequeestBody 接受json数据
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        User user1 = iUserService.loginByNameAndPwd(user.getUsername(),user.getPassword());
        Result<User> result = new Result<>();
        if (user1 != null){
            result.setData(user);
            result.setCode(StatusCode.SUCCESS);
            result.setMessage(Message.LOGIN_SUCCESS);
            //以用户名为key,value设为这个用户
            redisUtil.set(user.getUsername(),user);
        }else{
            result.setCode(StatusCode.DEFEAT);
            result.setMessage(Message.LOGIN_DEFEAT);
        }
        return result;
    }
}
